<template>
	<div class="test3">
		<v-scroll :on-refresh="refreshData" :on-infinite="loadData" ref="refresh">
			<!--<div v-if="isList(this.$route.params.newsId)">-->
				<div v-for="(item, index) in list">
					<news1 v-if="isFirst(index)" :item="item"></news1>
					<news2 v-else :item="item"></news2>
					<div class="dashed"></div>
				</div>
			<!--</div>-->
		</v-scroll>
		<!--<router-view></router-view>-->
		
	</div>
</template>

<script>
	import { mapState } from 'vuex'
	import store from '@/store'
	import Scroll from '@/components/common/refresh'
	import News1 from '@/components/main/news1'
	import News2 from '@/components/main/news2'

	export default {
		name: 'test3',
		data() {
			return {
				page: 1
			}
		},
		components: {
			news1: News1,
			news2: News2,
			'v-scroll': Scroll
		},
		computed: {
			...mapState({
				list: state => state.list,
			})
		},
		methods: {
			isFirst: function(index) {
				if(index == 0) {
					return true
				}
				return false
			},
			isList: function(id) {
				if(id == null) {
					return true
				}
				return false
			},
			onRefresh(done) {
				this.refreshData(done);
			},
			onInfinite(done) {
				this.loadData(done)
			},
			refreshData(done) {
				this.page = 1
				this.getData(done)
				//重置加载更多
				this.$refs.refresh.infiniteDone()
			},
			loadData(done) {
				this.page++
					this.getData(done)
			},
			getData(done) {
				this.$refs.refresh.setLoadMoreText("加载中...")
				console.log('get list data... page=', this.page, store.state.currentMenu.name)
				let url = 'http://vxinghe.com/m/index.php?c=home&a=lists&json=1&page=' + this.page + '&id=' + store.state.currentMenu.url//16
				this.$http.jsonp(url).then(function(response) {
					var arr = store.state.list
					let data = response.data
					if(this.page == 1) {
						arr = []
					}
					arr.push.apply(arr, data)
					store.commit('updateList', arr)
					console.log("get list data success")
					var str = "加载更多"
					if(arr.length == 0) {
						str = "暂无新闻"
					} else {
						if(data.length < 10) {
							str = "已加载全部"
						} else {
							done()
						}
					}
					this.$refs.refresh.setLoadMoreText(str)
				}).catch(function(response) {
					console.log("error: ", response)
				})
			}
		},
		mounted: function() {
			this.page = 1
			this.$refs.refresh.infinite()
		},
		watch: {
			'$route' (to, from) {
				this.$refs.refresh.infinite()
				this.page = 1
			}
		}
	}
</script>
